package com.tgky.ybzb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.entity.Rwal;
import com.tgky.ybzb.mapper.RwalMapper;
import com.tgky.ybzb.utils.MPUtil;
import com.tgky.ybzb.entity.Rwalj;
import com.tgky.ybzb.mapper.RwaljMapper;
import com.tgky.ybzb.service.IRwaljService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgky.ybzb.utils.TimeConvert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 任务案例集 服务实现类
 *
 * @author XYP
 * @since 2024-07-22
 */
@Service
public class RwaljServiceImpl extends ServiceImpl<RwaljMapper, Rwalj> implements IRwaljService {
    @Resource
    private RwaljMapper rwaljMapper;
    @Resource
    private RwalMapper rwalMapper;
    @Resource
    private RwalServiceImpl rwalService;

    /**
     * 通过实体类获取QueryWrapper
     * @param pageParam 分页参数
     * @param rwalj 实体信息
     * @return
     */
    @Override
    public QueryWrapper<Rwalj> getQueryWrapper(PageParam pageParam, Rwalj rwalj){
        QueryWrapper<Rwalj> queryWrapper = MPUtil.getQueryWrapper(pageParam);
        queryWrapper.orderByDesc("create_time");
        if(rwalj == null){
            return queryWrapper;
        }

        //主键
        if(rwalj.getId() != null){
            queryWrapper.lambda()
                    .eq(Rwalj::getId, rwalj.getId());
            return queryWrapper;
        }

        //TODO 此处可以根据各字段查询需求修改查询条件，eq、like、ge、gt、le、lt、ne...等等等
        queryWrapper.lambda()
                .eq(rwalj.getRwalmc() != null, Rwalj::getRwalmc, rwalj.getRwalmc())
                .eq(rwalj.getRwcj() != null, Rwalj::getRwcj, rwalj.getRwcj())
                .eq(rwalj.getRwssdlqy() != null, Rwalj::getRwssdlqy, rwalj.getRwssdlqy())
                .eq(rwalj.getRwansl() != null, Rwalj::getRwansl, rwalj.getRwansl());
        return queryWrapper;
    }

    /**
     * 查出任务案例集的分页数据
     * @param page
     * @param rwalj
     * @return
     */
    @Override
    public IPage<Rwalj> getRwaljListAndJcyb(Page page, Rwalj rwalj) {
        IPage<Rwalj> rwaljPage=rwaljMapper.selectPage(page,getQueryWrapper(null,rwalj));
        rwaljPage.getRecords().stream().forEach(obj -> {
            if(rwalj.getRwal()==null){
                rwalj.setRwal(new Rwal());
            }
            rwalj.getRwal().setSsjcybjID(obj.getId());
            List<Rwal> jcybList=rwalMapper.selectList(rwalService.getQueryWrapper(null,rwalj.getRwal()));
//            jcybList.stream().forEach(item->{
//                item.setGckssjStr(TimeConvert.convertDateTimeToStr(item.getGckssj()));
//                item.setGcjssjStr(TimeConvert.convertDateTimeToStr(item.getGcjssj()));
//                item.setZxdfwskStr(TimeConvert.convertDateTimeToStr(item.getZxdfwsk()));
//            });
            obj.setRwalList(jcybList);
        });
        return rwaljPage;
    }


}
